package com.gs.wzmanager.dao;

import com.gs.wzmanager.dao.mapper.UserMapper;
import com.gs.wzmanager.entity.Resource;
import com.gs.wzmanager.entity.Role;
import com.gs.wzmanager.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao {
    private Logger logger = LoggerFactory.getLogger(UserDao.class);

    @Autowired
    private UserMapper mapper;

    //新增用户
    public void addUser(User user) {
        try {
            mapper.addUser(user);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    //增加用户角色
    public void addUserRole(String userId,int roleId){
        try {
            mapper.addUserRole(userId,roleId);
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }


    //更新用户
    public void updateSimpleUser(User user) {
        try {
            mapper.updateSimpleUser(user);
        } catch (Exception e) {
            logger.error(e.getMessage());

        }
    }

    public User queryUserByPhone(String phone) {
        try {
            return mapper.queryUserByPhone(phone);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    public User queryUserById(String id) {
        try {
           return mapper.queryUserById(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    //根据用户名获取用户
    public List<User> queryUserByName(String name) {
        try {
            return mapper.queryUserByName(name);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    //根据某个用户获取全部角色
    public List<Role> queryRoleByUserId(String userId){
        try {
            return mapper.queryRoleByUserId(userId);
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }


    //根据用户id获取该用户可以访问的资源
    public List<Resource> queryResourceByUserId(String userId){
        try{
            return mapper.queryResourceByUserId(userId);
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    //获取全部角色
    public List<User> queryAllUser() {
        try {
            return mapper.queryAllUser();
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    //删除用户
    public void deleteUser(String userId) {
        try {
            mapper.deleteUser(userId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    public void deleteUserRoleByUserId(String userId){
        try {
            mapper.deleteUserRoleByUserId(userId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }


}
